package com.wusuowei.shiro_jwt.utils;

import com.wusuowei.shiro_jwt.model.po.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;
import org.apache.shiro.SecurityUtils;

import java.util.Objects;


/**
 * @author yangyouwang
 * @title: SecurityUtils
 * @projectName crud
 * @description: Security工具类
 * @date 2021/3/312:34 PM
 */
@Component
@Slf4j
public class SecurityUtil {






    /**
     * 获取当前登录用户名称
     * @return 用户名称
     */
    public static String getUserName() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated() || subject.isRemembered()) {
            // 确保Subject已认证或记住我状态
            Object principal = subject.getPrincipal();
            if (principal instanceof User) {
                User user = (User) principal;
                String username = user.getUsername();
                log.info("Security中的getUserName方法: " + username);
                return username;
            } else {
                log.warn("Principal不是User类型: " + principal);
            }
        } else {
            log.warn("Subject未认证或未记住状态");
        }
        return null;
    }


}
